package com.ycl.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ycl.common.BaseThread;
import com.ycl.common.Result;
import com.ycl.entity.Orders;
import com.ycl.service.OrdersService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * 订单
 */
@Slf4j
@RestController
@RequestMapping("/order")
public class OrdersController {
    @Autowired
    OrdersService ordersService;

    /**
     * 用户下单
     */
    @PostMapping("/submit")
    public Result<String> submit(@RequestBody Orders orders){
        log.info("order:{}",orders);
        ordersService.submit(orders);
        return Result.success("下单成功");
    }

    /**
     * 获得用户订单信息
     * @param page
     * @param pageSize
     * @return
     */
    @GetMapping("/userPage")
    public Result<Page<Orders>> page(int page,int pageSize){
        Page<Orders> pageInfo = new Page<>(page,pageSize);
        Long userId = BaseThread.getCurrentId();
        LambdaQueryWrapper<Orders> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Orders::getUserId,userId);
        ordersService.page(pageInfo, queryWrapper);
        return Result.success(pageInfo);
    }
}
